package com.oa.modules.approvalsec1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.Constant;
import com.oa.modules.approvalsec1.dao.ProjectFileDao;
import com.oa.modules.approvalsec1.entity.ProcessItemEntity;
import com.oa.modules.approvalsec1.entity.ProjectEntity;
import com.oa.modules.approvalsec1.entity.ProjectFileEntity;
import com.oa.modules.approvalsec1.service.ProjectFileService;
import com.oa.modules.approvalsec1.service.ProjectService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.Query;

@Log4j2
@Service("projectFileService")
public class ProjectFileServiceImpl extends ServiceImpl<ProjectFileDao, ProjectFileEntity> implements ProjectFileService {
    @Autowired
    private ProjectFileService projectFileService;

    @Autowired
    private ProjectService projectService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        params.put("status", Constant.YES_OR_NO.YES.getValue());
        IPage page = AppUtils.getPage(new Query<ProjectFileEntity>(), params);
        return AppUtils.setPage(page, baseMapper.findPage(params), baseMapper.findRecords(page, params));
    }

    @Override
    public List<ProjectFileEntity> queryAllFile(String id) {
        return baseMapper.queryAllFile(id);
    }

    @Override
    public void updateByProcess(String projectId, Integer processId, Integer newProcessId) {

        //有证照的项目节点集合
        List<ProjectFileEntity> itemList = baseMapper.findNodeByProcessId(projectId, processId);
        //新流程下的所有节点集合
        List<ProcessItemEntity> nodeList = baseMapper.findNodeByNewProcessId(newProcessId);
        LambdaUpdateWrapper<ProjectFileEntity> lambdaUpdateWrapper = null;
        for (ProjectFileEntity projectFileEntity : itemList) {
            lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            String nodeName = null;
            boolean flag = true;
            for (ProcessItemEntity processItemEntity : nodeList) {
                if (projectFileEntity.getNodeName().equals(processItemEntity.getName())) {
                    flag = false;
                    nodeName = processItemEntity.getName();
                    lambdaUpdateWrapper.set(ProjectFileEntity::getItemId, processItemEntity.getId())
                            .set(ProjectFileEntity::getItemName, nodeName).eq(ProjectFileEntity::getId, projectFileEntity.getId());
                    projectFileService.update(lambdaUpdateWrapper);
                    break;
                }
            }
            if (flag) {
                nodeName = projectFileEntity.getNodeName();
                lambdaUpdateWrapper.set(ProjectFileEntity::getItemId, null)
                        .set(ProjectFileEntity::getItemName, nodeName).eq(ProjectFileEntity::getId, projectFileEntity.getId());
                projectFileService.update(lambdaUpdateWrapper);
            }
        }
    }

    @Override
    public List tj() {
        ArrayList result = new ArrayList<>();
        result.add(baseMapper.tj());
        result.add(baseMapper.gtzzStateTj());
        result.add(baseMapper.bbldStateTj());
        ArrayList counts = new ArrayList<>();
        counts.add(projectService.count(new QueryWrapper<ProjectEntity>().eq("gtzz", 1)));
        counts.add(projectService.count(new QueryWrapper<ProjectEntity>().eq("bbld", 1)));
        counts.add(projectService.list().size());
        result.add(counts);
        result.add(this.baseMapper.projectCountByType());
        return result;
    }

    @Override
    public LinkedHashSet tjProject(String name) {
        List<HashMap> list = baseMapper.tjProject(name).stream().filter(e -> e != null).collect(Collectors.toList());
        LinkedHashSet result = new LinkedHashSet(list);
        return result;
    }

    @Override
    public List<ProjectEntity> bbldProject(Map<String, Object> params) {
        return baseMapper.bbldProject(params);
    }

}
